package com.example.wxordersystem.web;

import com.example.wxordersystem.converter.OrderForm2OrderDTOConverter;
import com.example.wxordersystem.dto.OrderDTO;
import com.example.wxordersystem.enums.ResultEnum;
import com.example.wxordersystem.exception.SellException;
import com.example.wxordersystem.form.OrderForm;
import com.example.wxordersystem.service.OrderService;
import com.example.wxordersystem.utils.ResultVOUtil;
import com.example.wxordersystem.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 常红伟
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    OrderService orderService;

    /**
     * 创建订单
     * @param orderForm
     * @return
     */
    @PostMapping("/create")
    public ResultVO create(@Valid OrderForm orderForm, BindingResult bindingResult){

        //验证orderFrom的参数是否符合约束
        if(bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确，orderFrom={}",orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

        //查看购物是否为空
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if (orderDTO.getOrderDetailList()==null){
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }

        OrderDTO ResultOrderDTO=orderService.create(orderDTO);
        Map <String,String>map = new HashMap<>();
        map.put("orderId",ResultOrderDTO.getOrderId());

        return ResultVOUtil.success(map);
    }

    /**
     * 查看订单列表
     * @param openId 用户ID
     * @param page  查看几页
     * @param size  每页显示多少
     * @return
     */
    @GetMapping("/list")
    public ResultVO list(@RequestParam(value = "openid") String openid,
                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                         @RequestParam(value = "size",defaultValue = "10") Integer size){
        System.out.println("--------------------------------------------------"+openid);
        List<OrderDTO> list=orderService.findList(openid,page+1,size);
        if (list==null){
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        return ResultVOUtil.success(list);
    }


    /**
     *查询订单详情
     * @param openId
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ResultVO detail(@RequestParam(value = "openid") String openid,@RequestParam(value = "orderId") String orderId){
        OrderDTO resultOrderDTO = orderService.findOne(orderId);
        System.out.println(resultOrderDTO.toString());
        //订单详情不存在
        if (resultOrderDTO.getOrderDetailList()==null){
            log.error("【查询订单详情错误】");
            throw new SellException(ResultEnum.ORDERDETAIL_NOT_EXIST);
        }

        //验证openid是否一致
        if(!resultOrderDTO.getBuyerOpenid().equals(openid)){
            log.error("【查询订单详情错误】不属于该用户");
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }
        return  ResultVOUtil.success(resultOrderDTO);
    }


    /**
     * 取消订单
     * @param openId
     * @param orderId
     * @return
     */
    @GetMapping("/cancel")
    public  ResultVO cancel(@RequestParam(value = "openid") String openId,@RequestParam(value = "orderId") String orderId){
        OrderDTO resultOrderDTO=orderService.findOne(orderId);

        //验证openid是否一致
        if(!resultOrderDTO.getBuyerOpenid().equals(openId)){
            log.error("【取消订单失败】不属于该用户");
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }

        OrderDTO orderDTO=orderService.cancel(resultOrderDTO);
        return ResultVOUtil.success();
    }

}
